import sys
import pygame
from settings import GameSetting
from ship import Ship
import game_function as gf
from pygame.sprite import Group


def run_game():
    # '初始化一个游戏，并创建一个屏幕对象'
    pygame.init()

    # 调用setting 并 配置
    ai_settings = GameSetting()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) 
    pygame.display.set_caption("alien_invasion")

    # 创建一个ship
    ship = Ship(ai_settings, screen)

    # 创建一个用于存储子弹的编组
    bullets = Group()

    # 开始游戏
    while True:

        # 监视键盘和鼠标事件
        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        bullets.update()

        # 删除消失的子弹
        for bullet in bullets.copy():
            if bullet.rect.bottom <= 0:
                bullets.remove(bullet)
        # 刷新屏幕
        gf.update_screen(ai_settings, screen, ship, bullets)


run_game()
